<template>
    <div id="dialog-container">
        <el-dialog v-dialogDrag 
            :title="dialogTitle" append-to-body
            :visible.sync="dialogVisible" @close="$emit('update:show', false)" :before-close="handleClose" :show="show"
            :close-on-click-modal="false" width="350px">
            <el-form :inline="true" size="small" :model="form" :rules="rules" ref="ruleForm" label-width="100px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="角色名称：" v-if="this.sort === 0" prop="rolename">
                            <el-input v-model="form.rolename"></el-input>
                        </el-form-item>
                        <el-form-item label="角色名称：" v-else prop="modifyname">
                            <el-input v-model="form.modifyname"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="addRole('ruleForm')">确 定</el-button>
                <el-button @click="cancel('ruleForm')">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { setRoleOperateService } from 'service/system-manage/role-permission-manage';
    export default {
        data() {
            return {
                dialogTitle: '新增角色',
                dialogVisible: this.show,
                form: {
                    rolename: '',
                    modifyname: ''
                },
                rules: {
                    rolename: [
                        { required: true, message: '请输入角色名称', trigger: 'blur' },
                    ],
                    modifyname: [
                        { required: true, message: '请输入角色名称', trigger: 'blur' },
                    ]
                }
            }
        },
        props: {
            show: {
                type: Boolean,
                default: false
            },
            // 判断弹框类型是新增或修改或详情
            sort: {
                type: Number,
                default: 0
            },
            // 角色名
            name: {
                type: String,
                default: ''
            },
            // 行id
            rowid: {
                type: String,
                default: ''
            }
        },
        watch: {
            show() {
                this.dialogVisible = this.show;
                // 动态显示dialog的title
                this.sort === 0 ? this.dialogTitle='新增角色' :  this.dialogTitle='修改角色';
                // 显示原本的角色名称
                this.sort === 1 ? this.form.modifyname = this.name :  '';
            } 
        },
        methods: {
            // 确定事件
            addRole(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        // 新增页面的参数
                        var roleName = this.form.rolename, roleId = "", type = "1";
                        // 修改页面的参数
                        if(this.sort == 1){
                            roleName = this.form.modifyname;
                            roleId = this.rowid;
                            type = "2";
                        }
                        setRoleOperateService(roleId, roleName, type)
                            .then((res) => {
                                // console.log(res)
                                this.$emit('updateTable', true);   // 将选择的账户信息传递到父页面
                            });
                        this.dialogVisible = false;
                    } else {
                        return false;
                    }
                });
            },
            // 取消
            cancel(formName) {
                this.$refs['ruleForm'].resetFields();   // 页面关闭时，将表单内容及验证重置
                this.dialogVisible = false;
            },
            handleClose() {
                this.dialogVisible = false;
                this.$refs['ruleForm'].resetFields();   // 页面关闭时，将表单内容及验证重置
            }
        }
    }
</script>

